package com.sskj.uboex.ui.fragment;


import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.uboex.R;
import com.sskj.uboex.bean.AssetBean;
import com.sskj.uboex.component.DaggerUserDataComponent;
import com.sskj.uboex.presenter.FinanceFragmentPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_FINANCE)//财务
public class FinanceFragment extends BaseFragment<FinanceFragmentPresenter> {
    @BindView(R.id.tvUSDTAll)
    TextView tvUSDTAll;
    @BindView(R.id.tvCNY)
    TextView tvCNY;
    @BindView(R.id.tvDetail)
    TextView tvDetail;
    @BindView(R.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R.id.tvUSDTFreeze)
    TextView tvUSDTFreeze;
    @BindView(R.id.btRecharge)
    Button btRecharge;
    @BindView(R.id.btWithdraw)
    Button btWithdraw;
    @BindView(R.id.tvAccountDetail)
    TextView tvAccountDetail;
    @BindView(R.id.ivSee)
    ImageView ivSee;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private boolean isSee;
    private AssetBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_finance;
    }

    @Override
    public FinanceFragmentPresenter getPresenter() {
        return new FinanceFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden && userViewModel != null) {
            userViewModel.update();
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (mPresenter != null) {
                    mPresenter.getAssets();
                }
            } else {
                userData = null;
                updateNoLogin();
            }
        });
        ClickUtil.click(tvDetail, () -> {
            if (userData == null) {
                ToastUtil.showShort("请先登录");
                return;
            }
            ARouter.getInstance().build(RConfig.APP_FINANCE_RECORD)
                    .withString(Constans.CODE, "USDT")
                    .navigation();

        });
        ClickUtil.click(tvAccountDetail, () -> {
            if (userData == null) {
                ToastUtil.showShort("请先登录");
                return;
            }
            ARouter.getInstance().build(RConfig.APP_FINANCE_RECORD)
                    .withString(Constans.CODE, "USDT")
                    .withInt(Constans.POS, 2)
                    .navigation();

        });
        ClickUtil.click(btWithdraw, () -> {
            if (userData == null) {
                ToastUtil.showShort("请先登录");
                return;
            }
        });

        ClickUtil.click(btRecharge, () -> {
            if (userData == null) {
                ToastUtil.showShort("请先登录");
                return;
            }
            ARouter.getInstance().build(RConfig.APP_RECHARGE)
                    .withString(Constans.TYPE, "2")
                    .withString(Constans.CODE, "USDT")
                    .navigation();
        });
        ClickUtil.click(ivSee, () -> {
            if (userData != null) {
                SPUtil.put(SPConfig.IS_SEE, !isSee);
                mPresenter.getAssets();
            }
        });
        isSee = SPUtil.get(SPConfig.IS_SEE, true);


    }


    /**
     * 未登录界面
     */
    private void updateNoLogin() {
        tvUSDTAll.setText("0.0000");
        tvUSDTFreeze.setText("0.0000");
        tvUSDT.setText("0.0000");
        tvCNY.setText("0.00");
    }

    @Override
    public void initData() {
    }

    public void updateUI(AssetBean assetBean) {
        if (assetBean == null) {
            return;
        }
        if (tvUSDTAll == null) {
            return;
        }
        data = assetBean;
        isSee = SPUtil.get(SPConfig.IS_SEE, true);
        tvUSDTAll.setText(!isSee ? "****" : assetBean.getUsableFund());
        tvUSDTFreeze.setText(!isSee ? "****" : assetBean.getFrostFund());
        tvUSDT.setText(!isSee ? "****" : assetBean.getInAllFee());
        tvCNY.setText("暂无");
        ClickUtil.click(btWithdraw, () -> {
            ARouter.getInstance().build(RConfig.APP_WITHDRAW)
                    .withString(Constans.MONEY, assetBean.getUsableFund())
                    .withString(Constans.CODE, "USDT")
                    .navigation();
        });
    }
}
